//
//  CustomTabBar.m
//  U_Airplane
//
//  Created by iOS on 2018/1/9.
//  Copyright © 2018年 cj. All rights reserved.
//

#import "CustomTabBar.h"

@implementation CustomTabBar

- (instancetype)init {
    
    self = [super init];
    if (self) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"StartLiving"] forState:UIControlStateNormal];
        btn.bounds = CGRectMake(0, 0, 64, 64);
        self.centerBtn = btn;
        [self addSubview:btn];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.05);
    
    UILabel *label = [[UILabel alloc] init];
//    label.text = @"发起直播";
    label.font = [UIFont systemFontOfSize:10];
    [label sizeToFit];
    label.textColor = [UIColor grayColor];
    [self addSubview:label];
    label.centerX = self.centerBtn.centerX;
    label.centerY = CGRectGetMaxY(self.centerBtn.frame) +6;
    
    int index = 0;
    int cout = 0;
    CGFloat width = self.bounds.size.width / 3;
    for (UIView *sub in self.subviews) {
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (cout>=2) {
                sub.hidden = YES;
            }
            cout++;
            sub.frame = CGRectMake(index*width, self.bounds.origin.y, width, self.bounds.size.height - 2);
            index ++;
            if (index == 1) {
                index ++ ;
            }
            if(index==3){
                index = 0;
            }
        }
    }
}

-(void)setIndex:(NSInteger)index{
    
    _index = index;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"postNotification" object:@(index) userInfo:nil];
    });
    
    NSMutableArray *muarray = [NSMutableArray array];
    for (int a=0; a<self.subviews.count; a++) {
        id sub = self.subviews[a];
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [muarray addObject:sub];
        }
    }
    
    if (index==0&&muarray.count>=3) {
        UIView *view = muarray[2];
        UIView *viewW = muarray[0];
        view.hidden = YES;
        viewW.hidden = NO;
    }else if(index==1&&muarray.count>=4){
        UIView *view = muarray[3];
        UIView *viewW = muarray[1];
        view.hidden = YES;
        viewW.hidden = NO;
    }else if(index==2&&muarray.count>=3){
        UIView *view = muarray[0];
        UIView *viewW = muarray[2];
        view.hidden = YES;
        viewW.hidden = NO;
    }else if(index==3&&muarray.count>=4){
        UIView *view = muarray[1];
        UIView *viewW = muarray[3];
        view.hidden = YES;
        viewW.hidden = NO;
    }
}



- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    
    if (self.isHidden == NO) {
        CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
        if ([self.centerBtn pointInside:newPoint withEvent:event]) {
            return  self.centerBtn;
        }else{
            return [super hitTest:point withEvent:event];
        }
    }else{
        return [super hitTest:point withEvent:event];
    }
}

@end
